package com.dlxj.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.dlxj.entity.Patroltask;
import com.dlxj.entity.Receiptpatrol;
import com.dlxj.entity.User;
import com.dlxj.service.PatrolTaskService;
import com.dlxj.service.UserService;
import com.dlxj.util.ConstClass;
import com.dlxj.util.MyJSON;
import com.dlxj.util.PageBean;

public class PatrolTaskAction {
	String json;
	String uid;
	Patroltask patroltask;
	PatrolTaskService patrolTaskService;
	UserService userService;
	Receiptpatrol receiptpatrol;
	String state;
	String pid;
	Date sd;
	Date ed;
	int pageIndex;
	int pageSize;

	public String addTask() {
		List<User> users = new ArrayList<User>();
		int code=0;
		if (uid != null && !uid.equals("")) {
			String[] ids = uid.split(",");
			for (String id : ids) {
				users.add(userService.findById(Integer.valueOf(id)));
			}
		}
		 try {
			code = patrolTaskService.addPatrolTast(patroltask, users);
		} catch (Exception e) {
			e.printStackTrace();
		}
		json = "{code:" + code + "}";
		return "success";
	}

	public String findWaitState() {

		List<Patroltask> patroltasks = patrolTaskService.findWaitStatePatrol();
		json = MyJSON.toJson(patroltasks, "circuitriesForStartpoleNo",
				"circuitriesForEndpoleNo").toString();
		return "success";
	}

	public String findAssgined() {
		List<Patroltask> patroltasks = patrolTaskService.findAssignedList();
		json = MyJSON.toJson(patroltasks, "circuitriesForStartpoleNo",
				"circuitriesForEndpoleNo").toString();
		return "success";
	}

	public String findExecuting() {
		List<Patroltask> patroltasks = patrolTaskService.findExecutingList();
		json = MyJSON.toJson(patroltasks, "circuitriesForStartpoleNo",
				"circuitriesForEndpoleNo").toString();
		return "success";
	}

	public String findFinish() {
		List<Patroltask> patroltasks = patrolTaskService.findFinishList();
		json = MyJSON.toJson(patroltasks, "circuitriesForStartpoleNo",
				"circuitriesForEndpoleNo").toString();
		return "success";
	}

	public String findTaskNoByPid() {
		List<Patroltask> patroltasks = patrolTaskService.findTaskNoByPid(state,
				pid);
		json = MyJSON.toJson(patroltasks, "circuitriesForStartpoleNo",
				"circuitriesForEndpoleNo").toString();
		return "success";

	}

	public String findByTaskNo() {
		String[] pids = pid.split(",");
		List<Patroltask> patroltasks = new ArrayList<Patroltask>();
		for (String no : pids) {
			Patroltask tmp = patrolTaskService.findByTaskNo(no);
			patroltasks.add(tmp);
		}

		json = MyJSON.toJson(patroltasks, "circuitriesForStartpoleNo",
				"circuitriesForEndpoleNo").toString();
		return "success";

	}

	public String findTaskNoByPidAndUid() {
		List<Patroltask> patroltasks = patrolTaskService.findTaskNoByPidAndUid(
				state, pid, ConstClass.getCurrentUser().getId());
		json = MyJSON.toJson(patroltasks, "circuitriesForStartpoleNo",
				"circuitriesForEndpoleNo").toString();
		return "success";

	}

	public String executeTask() {
		int code = patrolTaskService.executeTask(pid);
		json = "{code:" + code + "}";

		return "success";

	}

	public String assignUser() {
		int code = patrolTaskService.assignUser(pid, uid);
		json = "{code:" + code + "}";
		return "success";

	}

	public String statistics() {

		PageBean bean = patrolTaskService.statistics(patroltask, receiptpatrol,
				sd, ed, pageIndex, pageSize);
		json = MyJSON.toJson(bean, "circuitriesForStartpoleNo",
				"circuitriesForEndpoleNo").toString();
		return "success";
	}

	public String export() {
		String name = patrolTaskService.export(patroltask, receiptpatrol, sd,
				ed);
		json = "{\"file\" :\"" + name + "\"}";
		return "success";
	}

	public String sure() {
		int code = patrolTaskService.sureTask(pid);
		json = "{code:" + code + "}";
		return "success";
	}

	public String getJson() {
		return json;
	}

	public void setJson(String json) {
		this.json = json;
	}

	public String getUid() {
		return uid;
	}

	public void setUid(String uid) {
		this.uid = uid;
	}

	public Patroltask getPatroltask() {
		return patroltask;
	}

	public void setPatroltask(Patroltask patroltask) {
		this.patroltask = patroltask;
	}

	public PatrolTaskService getPatrolTaskService() {
		return patrolTaskService;
	}

	public void setPatrolTaskService(PatrolTaskService patrolTaskService) {
		this.patrolTaskService = patrolTaskService;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public String getPid() {
		return pid;
	}

	public void setPid(String pid) {
		this.pid = pid;
	}

	public Receiptpatrol getReceiptpatrol() {
		return receiptpatrol;
	}

	public void setReceiptpatrol(Receiptpatrol receiptpatrol) {
		this.receiptpatrol = receiptpatrol;
	}

	public Date getSd() {
		return sd;
	}

	public void setSd(Date sd) {
		this.sd = sd;
	}

	public Date getEd() {
		return ed;
	}

	public void setEd(Date ed) {
		this.ed = ed;
	}

	public int getPageIndex() {
		return pageIndex;
	}

	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

}
